package com.wenhuitec.productlib.ui.adapter.sku;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.NzProductCategory;
import com.wenhuitec.productlib.R;
import com.wenhuitec.productlib.entity.Sku;
import com.wenhuitec.productlib.ui.adapter.CategorySortAdapter;
import com.wenhuitec.sysui.recyclerviewHelper.IItemTouchHelperAdapter;
import com.wenhuitec.sysui.recyclerviewHelper.OnStartDragListener;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

/**
 * 商品规格中,遍历商品
 */
public class SkuEditAdapter extends RecyclerView.Adapter<SkuEditAdapter.ViewHolder> {
    private Context context;
    private List<Sku> list;
    private OnClickSwitchListener mOnClickSwitchListener;
    private final OnStartDragListener mDragListener;

    public SkuEditAdapter(Context context, List<Sku> list, OnStartDragListener mDragListener) {
        this.context = context;
        this.list = list;
        this.mDragListener = mDragListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_sku_edit, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
        viewHolder.skuNameEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
                    String name = _v.getText().toString();
                    list.get(position).setName(name);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        viewHolder.skuPriceEd.setOnFocusChangeListener((View v, boolean hasFocus) -> {
            try {
                if (!hasFocus) {
                    EditText _v = (EditText) v;
                    String price = _v.getText().toString();
                    list.get(position).setPrice(price);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        EditText skuNameEd;

        EditText skuPriceEd;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            skuNameEd = itemView.findViewById(R.id.skuNameEd);
            skuPriceEd = itemView.findViewById(R.id.skuPriceEd);

        }
    }

    public void setOnClickSwitchListener(OnClickSwitchListener onClickSwitchListener) {
        mOnClickSwitchListener = onClickSwitchListener;
    }

    public interface OnClickSwitchListener {
        void onClick(int position, boolean isChecked);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addList() {
        Sku sku = new Sku();
        list.add(sku);
        this.notifyDataSetChanged();
    }

    public List<Sku> getSkuList() {
        return list;
    }


}

